package com.oocl.shopping.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import com.oocl.shopping.dao.BookDao;
import com.oocl.shopping.pojo.Book;
import com.oocl.shopping.repository.BooksDB;
import com.oocl.shopping.util.DBUtil;

public class BookDaoImpl implements BookDao{

	@Override
	public Map<String, Book> findAll() {
		String sql = "select * from books";
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			con=DBUtil.getConnection();
			pst = con.prepareStatement(sql);
			
			rs = pst.executeQuery();
			while (rs.next()) {
				Book book = new Book(rs.getString("id"), rs.getString("name"), rs.getString("path"));
				BooksDB.getBooksMap().put(rs.getString("id"), book);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.close(null, pst, rs);
		}
		return BooksDB.getBooksMap();
	}

}
